package com.kehutong.api.action;

import com.kehutong.common.Authutil.OAuth2Provider;
import com.kehutong.common.entity.OAuthClient;
import com.kehutong.common.util.Token;
import org.coraframework.authz.HasPermission;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.execute.entity.FindPage;
import org.coraframework.util.Objects;
import org.coraframework.util.secret.MD5;

@MvcAction
@ReqMapping("/admin/oau-kht/configure")
public class ConfigureAction {
    @Inject
    private JdbcSession sqlSession;
    public final static String[] EXCEL_IMPORT_HEADER = {"归属公司编号","归属公司名称","应用id","应用secret"};

    @HasPermission("oau-kht:configure")
    @ReqMapping("/list")
    public Object list(Token token, JSONObject jsonObject) {
        FindPage<OAuthClient> find = sqlSession.findPage(OAuthClient.class)
                .eq(jsonObject, "companyNo",  "companyName", "appid")
                .like(jsonObject, "companyName")
                .eq("deleted", false)
                .order("createTime", "DESC")
                .page(jsonObject);
        return find.exe();
    }
    @HasPermission("oau-kht:configure:save")
    @Bean(newInstance=true, copy=true)
    @ReqMapping("/save")
    public Object save(OAuthClient oAuthClient) {
        // 增加校验不允许有相同的小区名称
        OAuthClient item = sqlSession.findOne(OAuthClient.class)
                .eq("companyNo", oAuthClient.getCompanyNo())
                .eq("deleted", false)
                .exe();
        if(Objects.nonNull(item)){
            return Result.error(0, "已有当前企业编号，请修改后重试");
        }
        //生成APPID
        String appId= OAuth2Provider.getRandom();
        //生成secret
        String code_data = appId + System.nanoTime();
        String secret =  MD5.MD5Encode(code_data);
        oAuthClient.setAppid(appId);
        oAuthClient.setSecret(secret);
        oAuthClient.setId(appId);
        sqlSession.insert(oAuthClient);
        return Result.success(oAuthClient);
    }
    @HasPermission("oau-kht:configure:edit")
    @Bean(copy=true)
    @ReqMapping("/update")
    public Object update(OAuthClient oAuthClient) throws Exception {
        // 增加校验不允许有相同归属公司名称
        OAuthClient item = sqlSession.findOne(OAuthClient.class)
                .eq("companyName", oAuthClient.getCompanyName())
                .eq("deleted", false)
                .exe();

        if(Objects.nonNull(item)){
            return Result.error(0, "已有当前归属公司名称，请修改后重试");
        }
        //生成APPID
        String appId= OAuth2Provider.getRandom();
        //生成secret
        String code_data = appId + System.nanoTime();
        String secret =  MD5.MD5Encode(code_data);
        oAuthClient.setAppid(appId);
        oAuthClient.setSecret(secret);
        sqlSession.updateById(oAuthClient);
        return Result.success();
    }

    @HasPermission("oau-kht:configure:delete")
    @ReqMapping("/delete")
    public Object deleteById(OAuthClient oAuthClient) {
        if (oAuthClient != null) {
            oAuthClient.setDeleted(Boolean.TRUE);
            sqlSession.updateById(oAuthClient);
        }

        return Result.success();
    }

    @HasPermission("oau-kht:configure")
    @ReqMapping("/get")
    public Object get(String id) {
        return Result.success(sqlSession.findById(OAuthClient.class, id));
    }

}
